# Define Macro for adding tests

IF(${PACKAGE_NAME}_ENABLE_SHARED)
  SET( TEST_DEFINES "${MESQUITE_DEFINES}" )
ELSE()
  SET( TEST_DEFINES "${MESQUITE_DEFINES} -DMESQUITE_STATIC_LIB" )
ENDIF()

MACRO(MSQ_ADD_TEST_PARALLEL name exe nproc)
  IF (NOT ${PROJECT_NAME} STREQUAL "Mesquite")
    add_executable( ${exe} SOURCES ${ARGN} )
    ADD_TEST( ${exe} NAME ${name} NUM_MPI_PROCS ${nproc} )
    #TARGET_LINK_LIBRARIES( ${exe} mesquite )
  ELSE(NOT ${PROJECT_NAME} STREQUAL "Mesquite")
    IF (${PACKAGE_NAME}_ENABLE_TESTS)
      ADD_EXECUTABLE( ${exe} ${ARGN} )
      ADD_TEST( ${name} ${EXECUTABLE_OUTPUT_PATH}/${exe} )
      TARGET_LINK_LIBRARIES( ${exe} mesquite )
    ENDIF(${PACKAGE_NAME}_ENABLE_TESTS)
  ENDIF(NOT ${PROJECT_NAME} STREQUAL "Mesquite")
  SET_SOURCE_FILES_PROPERTIES(${ARGN} COMPILE_FLAGS "-DSRCDIR=\"\\\"${CMAKE_CURRENT_SOURCE_DIR}/\\\"\" ${TEST_DEFINES}")
ENDMACRO(MSQ_ADD_TEST_PARALLEL)

MACRO(MSQ_ADD_TEST name exe)
  MSQ_ADD_TEST_PARALLEL( ${name} ${exe} 1 ${ARGN} )
ENDMACRO(MSQ_ADD_TEST)


# Create meshfiles.h
IF(NOT DEFINED ${PACKAGE_SOURCE_DIR})
  SET(PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
ENDIF()
SET(meshfiles_h_contents "#ifndef MESH_FILES_DIR\n# define MESH_FILES_DIR \"${PACKAGE_SOURCE_DIR}/MeshFiles/mesquite/\"\n#endif\n" )
IF(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/meshfiles.h)
  FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/meshfiles.h ${meshfiles_h_contents} )
ENDIF()
INCLUDE_DIRECTORIES(REQUIRED_DURING_INSTALLATION_TESTING ${CMAKE_CURRENT_BINARY_DIR})


# All tests are in subdirs
MSQ_ADD_TEST( 2d_formulation 2d_formulation_main main.cpp )
MSQ_ADD_TEST( 2d_metrics 2d_metrics_main main.cpp )
MSQ_ADD_TEST( 2d_target 2d_target_main main.cpp )
MSQ_ADD_TEST( ActiveSetTest ActiveSetTest_main main.cpp )
MSQ_ADD_TEST( algorithm_test algorithm_test_main main.cpp )
MSQ_ADD_TEST( analytical_grad_3D analytical_grad_3D_main main.cpp )
MSQ_ADD_TEST( benchmark_tests benchmark_tests_main benchmark_tests.cpp )
MSQ_ADD_TEST( headers headers_main headers_test.cpp )
MSQ_ADD_TEST( high_aspect_ratio high_aspect_ratio_main main.cpp )
ADD_SUBDIRECTORY(higher_order)
MSQ_ADD_TEST( idft_time idft_time_main main.cpp )
MSQ_ADD_TEST( jacobi_1 jacobi_1_main jacobi_1.cpp )
MSQ_ADD_TEST( laplacian_test laplacian_test_main main.cpp )
MSQ_ADD_TEST( laplacian_polygon_test laplacian_polygon_test_main main.cpp )
MSQ_ADD_TEST( nongradient_test nongradient_test_main main.cpp )
MSQ_ADD_TEST( paraboloid_domain_test paraboloid_domain_test_main main.cpp )
MSQ_ADD_TEST( pyramid pyramid_main main.cpp )
MSQ_ADD_TEST( simple_hybrid_test simple_hybrid_test_main main.cpp )
ADD_SUBDIRECTORY(slaved)
MSQ_ADD_TEST( synchronous synchronous_main main.cpp )
MSQ_ADD_TEST( feasible_newton feasible_newton_test feasible_newton_test.cpp )
MSQ_ADD_TEST( transform transform_main main.cpp )
MSQ_ADD_TEST( untangle_test untangle_test_main main.cpp )
MSQ_ADD_TEST( wedge wedge_main main.cpp )
ADD_SUBDIRECTORY(wrapper_tests)

IF (TPL_ENABLE_MPI)
  ADD_SUBDIRECTORY(parallel_untangle_shape)
  ADD_SUBDIRECTORY(parallel_smooth_laplace)
ENDIF()

IF(${PACKAGE_NAME}_ENABLE_IMESH)
  ADD_SUBDIRECTORY(imesh)
ENDIF()

IF(${PACKAGE_NAME}_ENABLE_IGEOM)
  ADD_SUBDIRECTORY(igeom)
ENDIF()

IF (NOT ${PROJECT_NAME} STREQUAL "Mesquite")
  IF(${PACKAGE_NAME}_ENABLE_CppUnit)
    SET(CPPUNIT_LIBRARY ${TPL_CppUnit_LIBRARIES})
    SET(CPPUNIT_INCLUDES ${TPL_CppUnit_INCLUDE_DIRS})
  ELSE()
    SET(CPPUNIT_LIBRARY "CPPUNIT_LIBRARY-NOTFOUND")
    SET(CPPUNIT_INCLUDES "CPPUNIT_INCLUDES-NOTFOUND")
  ENDIF()
ELSE()
  # Check for libraries
  FIND_LIBRARY( CPPUNIT_LIBRARY NAMES cppunit )
  FIND_PATH( CPPUNIT_INCLUDES cppunit/extensions/HelperMacros.h )
ENDIF()

# Cannot build unit tests with Windows DLL because
# internal symbols are not exposed
IF (EXISTS ${CPPUNIT_LIBRARY} AND IS_DIRECTORY ${CPPUNIT_INCLUDES})
  IF(WIN32 OR WIN64)
    IF(NOT ${PACKAGE_NAME}_ENABLE_SHARED)
      ADD_SUBDIRECTORY(unit)
    ENDIF()
  ELSE()
    ADD_SUBDIRECTORY(unit)
  ENDIF()
ENDIF()
